﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SLWebApp.ChildWindows
{
    public partial class ChildWindow_VesselBerthes : ChildWindow
    {
        ServiceReference_HB.ServiceForSLClient clientService = new ServiceReference_HB.ServiceForSLClient();

        ObservableCollection<ServiceReference_HB.UM_Ref_Code> mListRef_Code = null;//装卸标志.

        /// <summary>
        /// 是否新增还是修改，为true表示是新增。
        /// </summary>
        public bool Is_Add_Modefy = false;
        /// <summary>
        /// 保存船期的ID。
        /// </summary>
        public string mSCD_ID = string.Empty;

        /// <summary>
        /// 当前登陆人员ID。
        /// </summary>
        public string mUser_ID = string.Empty;

        //当前靠泊数据ID。
        public string mVBT_ID = string.Empty;

        public event MouseButtonEventHandler OnAdded_ModefyData;

        //当前选择的靠泊数据。
        public ServiceReference_HB.UM_Vessel_Berthes mVessel_Berthes = null;

        public ChildWindow_VesselBerthes()
        {
            InitializeComponent();
            mListDown.Clear();
            progressbar.IsBusy = true;
            //得到所有码头标志。
            clientService.GetAllTerminallsAsync();
            clientService.GetAllTerminallsCompleted += new EventHandler<ServiceReference_HB.GetAllTerminallsCompletedEventArgs>(clientService_GetAllTerminallsCompleted);
            //得到装卸标志。
            clientService.GetRefCodeAsync(" rv_domain='LDUNLD_FG' ", string.Empty);
            clientService.GetRefCodeCompleted += new EventHandler<ServiceReference_HB.GetRefCodeCompletedEventArgs>(clientService_GetRefCodeCompleted);
        }

        void clientService_GetRefCodeCompleted(object sender, ServiceReference_HB.GetRefCodeCompletedEventArgs e)
        {
            mListRef_Code = e.Result as ObservableCollection<ServiceReference_HB.UM_Ref_Code>;
            this.comboxLDUNLDFG.ItemsSource = mListRef_Code;
            ClearProgressBar(true);
        }

        //修改时初始化数据.
        public void Inited()
        {
            if (progressbar.IsBusy)
            {
                return;
            }
            if (mVessel_Berthes != null && list_Terminal != null)
            {
                List<ServiceReference_HB.UM_Terminals> result = (from table in list_Terminal
                                                                 where table.CST_CSTM == mVessel_Berthes.CST_CSTM
                                                                 select table).ToList();
                if (result != null && result.Count > 0)
                {
                    this.comboxTerminal.SelectedItem = result[0];
                }
                //装卸标志。
                List<ServiceReference_HB.UM_Ref_Code> result_fg = (from table in mListRef_Code
                                                                   where table.RV_LOW_VALUE == mVessel_Berthes.VBT_LDUNLDFG
                                                                   select table).ToList();
                if (result_fg != null && result_fg.Count > 0)
                {
                    comboxLDUNLDFG.SelectedItem = result_fg[0];
                }

                this.txtArriveF.Text = mVessel_Berthes.VBT_PBTHDRAFT == null ? "0" : mVessel_Berthes.VBT_PBTHDRAFT.Value.ToString();
                this.txtArriveM.Text = mVessel_Berthes.VBT_PBTHDRAFT_P == null ? "0" : mVessel_Berthes.VBT_PBTHDRAFT_P.Value.ToString();
                this.txtArriveL.Text = mVessel_Berthes.VBT_PBTHDRAFT_A == null ? "0" : mVessel_Berthes.VBT_PBTHDRAFT_A.Value.ToString();
                this.txtLeaveF.Text = mVessel_Berthes.VBT_PDPTDRAFT == null ? "0" : mVessel_Berthes.VBT_PDPTDRAFT.Value.ToString();
                this.txtLeaveM.Text = mVessel_Berthes.VBT_PDPTDRAFT_P == null ? "0" : mVessel_Berthes.VBT_PDPTDRAFT_P.Value.ToString();
                this.txtLeaveL.Text = mVessel_Berthes.VBT_PDPTDRAFT_A == null ? "0" : mVessel_Berthes.VBT_PDPTDRAFT_A.Value.ToString();
            }
        }

        /// <summary>
        /// 所有码头。
        /// </summary>
        ObservableCollection<ServiceReference_HB.UM_Terminals> list_Terminal = null;
        void clientService_GetAllTerminallsCompleted(object sender, ServiceReference_HB.GetAllTerminallsCompletedEventArgs e)
        {

            list_Terminal = e.Result as ObservableCollection<ServiceReference_HB.UM_Terminals>;
            comboxTerminal.ItemsSource = list_Terminal;
            ClearProgressBar(true);
            // Inited();
        }

        //保存
        private void btnSave_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ServiceReference_HB.UM_Terminals terminal = comboxTerminal.SelectedItem as ServiceReference_HB.UM_Terminals;
            if (terminal == null)
            {
                return;
            }
            int arriveF, arriveM, arriveL = 0;
            int leaveF, leaveM, leaveL = 0;

            int.TryParse(this.txtArriveF.Text, out arriveF);
            int.TryParse(this.txtArriveM.Text, out arriveM);
            int.TryParse(this.txtArriveL.Text, out arriveL);

            int.TryParse(this.txtLeaveF.Text, out leaveF);
            int.TryParse(this.txtLeaveM.Text, out leaveM);
            int.TryParse(this.txtLeaveL.Text, out leaveL);
            ServiceReference_HB.UM_Ref_Code ref_code = this.comboxLDUNLDFG.SelectedItem as ServiceReference_HB.UM_Ref_Code;
            string P_VBT_LDUNLDFG = string.Empty;
            if (ref_code != null)
            {
                P_VBT_LDUNLDFG = ref_code.RV_LOW_VALUE;
            }
            if (Is_Add_Modefy)
            {// 新增:--靠泊、离泊时间不要，就传一个最小值。
                clientService.InsertUpdateVesselBerthAsync(1, mSCD_ID, DateTime.MinValue,
                    DateTime.MinValue, txtArriveF.Text, txtArriveM.Text, txtArriveL.Text, txtLeaveF.Text,
                    txtLeaveM.Text, txtLeaveL.Text, terminal.CST_ID.ToString(), mUser_ID, null, P_VBT_LDUNLDFG);
            }
            else
            {//修改:--靠泊、离泊时间不要，就传一个最小值。
                clientService.InsertUpdateVesselBerthAsync(2, mSCD_ID, DateTime.MinValue,
                    DateTime.MinValue, txtArriveF.Text, txtArriveM.Text, txtArriveL.Text, txtLeaveF.Text,
                    txtLeaveM.Text, txtLeaveL.Text, terminal.CST_ID.ToString(), mUser_ID, mVBT_ID, P_VBT_LDUNLDFG);
            }
            //通知主窗口更新数据。
            clientService.InsertUpdateVesselBerthCompleted -= new EventHandler<ServiceReference_HB.InsertUpdateVesselBerthCompletedEventArgs>(clientService_InsertUpdateVesselBerthCompleted);
            clientService.InsertUpdateVesselBerthCompleted += new EventHandler<ServiceReference_HB.InsertUpdateVesselBerthCompletedEventArgs>(clientService_InsertUpdateVesselBerthCompleted);
        }

        void clientService_InsertUpdateVesselBerthCompleted(object sender, ServiceReference_HB.InsertUpdateVesselBerthCompletedEventArgs e)
        {
             string str = e.Result.Trim();
             if (str.Substring(0, 1) == "1")
             {
                 // MessageBox.Show(str.Substring(2, str.Length - 2));
                 if (!Is_Add_Modefy && mVessel_Berthes != null)
                 {// 新增:--靠泊、离泊时间不要，就传一个最小值。

                     ServiceReference_HB.UM_Terminals terminal = comboxTerminal.SelectedItem as ServiceReference_HB.UM_Terminals;
                     if (terminal == null)
                     {
                         return;
                     }
                     int arriveF, arriveM, arriveL = 0;
                     int leaveF, leaveM, leaveL = 0;

                     int.TryParse(this.txtArriveF.Text, out arriveF);//前吃水。
                     int.TryParse(this.txtArriveM.Text, out arriveM);//平吃水。
                     int.TryParse(this.txtArriveL.Text, out arriveL);//后吃水。

                     int.TryParse(this.txtLeaveF.Text, out leaveF);
                     int.TryParse(this.txtLeaveM.Text, out leaveM);
                     int.TryParse(this.txtLeaveL.Text, out leaveL);
                     ServiceReference_HB.UM_Ref_Code ref_code = this.comboxLDUNLDFG.SelectedItem as ServiceReference_HB.UM_Ref_Code;
                     string P_VBT_LDUNLDFG = string.Empty;
                     if (ref_code != null)
                     {
                         P_VBT_LDUNLDFG = ref_code.RV_LOW_VALUE;
                     }
                     mVessel_Berthes.CST_CSTM = terminal.CST_CSTM;//靠泊码头
                     mVessel_Berthes.VBT_LDUNLDFG = ref_code.RV_LOW_VALUE;
                     mVessel_Berthes.VBT_PBTHDRAFT = arriveF;
                     mVessel_Berthes.VBT_PBTHDRAFT_P = arriveM;
                     mVessel_Berthes.VBT_PBTHDRAFT_A = arriveL;
                     mVessel_Berthes.VBT_PDPTDRAFT = leaveF;
                     mVessel_Berthes.VBT_PDPTDRAFT_P = leaveM;
                     mVessel_Berthes.VBT_PDPTDRAFT_A = leaveL;
                 }
                 else
                 {//新增。
                     OnAdded_ModefyData(sender, null);
                 }
             }
             else
             {
                 MessageBox.Show(e.Result);
             }
            this.Close();
        }

        List<bool> mListDown = new List<bool>();
        private void ClearProgressBar(bool isDown)
        {
            if (mListDown.Count == 1)
            {
                progressbar.IsBusy = false;
                Inited();//第一次加载数据时初始化。
            }
            else
            {
                mListDown.Add(isDown);
            }
        }

        /// <summary>
        ///  清除页面上的内容。
        /// </summary>
        private void ClearContent()
        {
            this.comboxLDUNLDFG.SelectedItem = null;
            comboxTerminal.SelectedItem = null;
            txtArriveF.Text = "";
            txtArriveL.Text = "";
            txtArriveM.Text = "";
            txtLeaveF.Text = "";
            txtLeaveL.Text = "";
            txtLeaveM.Text = "";
        }

        //窗体关闭事件。
        private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ClearContent();
        }


    }//end class
}

